<?php
/*
 * Created on 2013-2-6
 *
 */
include_once dirname(__FILE__)."/basedao.php";
include_once dirname(__FILE__)."/../objects/role.php";
class RolesDAO extends BaseDAO{
	/**
	 * 增加一个角色
	 */
	public function insertRole($role){
		$this->getConn()->query("LOCK TABLES roles WRITE");
		$sqlstr = "INSERT INTO roles(role_name, role_limit, role_description) VALUES(?,?,?)";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("sis",
				$role->getRoleName(),
				$role->getRoleLimit(),
				$role->getRoleDescription());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	 * 更新一个角色信息
	 */
	public function updateRole($role){
		$this->getConn()->query("LOCK TABLES roles WRITE");
		$sqlstr = "UPDATE roles SET role_name=?, role_limit=?, role_description=? WHERE role_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("sssssi",
				$role->getRoleName(),
				$role->getRoleLimit(),
				$role->getRoleDescription(),
				$role->getUserId());
		$stmt->execute();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
	}
	/**
	  * 根据角色ID查找一个角色
	  */
	 public function getRoleInfoById($role_id){
		$this->getConn()->query("LOCK TABLES roles READ");
		$sqlstr = "SELECT role_id, role_name, role_limit, role_description FROM roles WHERE role_id=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("i", $role_id);
		$stmt->execute();
		$role = new Role();
		$stmt->bind_result($role_id, $role_name, $role_limit, $role_description);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$role->setRoleId($role_id);
		$role->setRoleName($role_name);
		$role->setRoleLimit($role_limit);
		$role->setRoleDescriptiion($role_description);
		return $role;
	 }
	/**
	  * 根据角色名查找一个角色
	  */
	public function getRoleInfoByName($role_name){
		$this->getConn()->query("LOCK TABLES roles READ");
		$sqlstr = "SELECT role_id, role_name, role_limit, role_description FROM roles WHERE role_name=?";
		$stmt = $this->getStmt();
		$stmt->prepare($sqlstr);
		$stmt->bind_param("s", $role_name);
		$stmt->execute();
		$role = new Role();
		$stmt->bind_result($role_id, $role_name, $role_limit, $role_description);
		$stmt->fetch();
		$stmt->close();
		$this->getConn()->query("UNLOCK TABLES");
		$role->setRoleId($role_id);
		$role->setRoleName($role_name);
		$role->setRoleLimit($role_limit);
		$role->setRoleDescriptiion($role_description);
		return $role;
	}
}
?>
